import json
import configparser
import requests
from lxml import etree

from Cpudata import GetCpu as GC
from Mainboarddata import GetMainboard as GM
from GraphicsCarddata import GetGraphicsCard as GGC
from Memorydata import GetMemory as GME
from HardDiskdata import GetHardDisk as GH


#配置文件地址
config_path = './config/config.ini'

#读取config配置数据
config = configparser.ConfigParser()
config.read(config_path)
mode = config.get('TEXTMODE','MODE')   #获取测试是否开启

header = config.get('NETWORK','headers')   #获取报头，一般不做修改
cookies_path = config.get('NETWORK','cookies')   #获取cookie，若无法运行，去网站手动获取即可

#读取cookie
with open(cookies_path, 'r') as file:
    cookie = file.read()

#测试环境
if mode == 'True':
    mode = 1
else:
    mode = 0
mode = bool(mode)

#↓单独关闭此页调试日志
#mode = False

#json文件位置
json_path = './data/url.json'

with open(json_path,'r') as file:
    data = json.load(file)

headers = {
    'User-Agent': header
}

cookies = {
    'Cookie': cookie
}

def get_data_cpu_page():
    cpu_urls = []
    page = [1,2,3,4,5,6,7]
    if mode:
        page = [1]
    for i in page:
        url = data['cpu']
        url = url.format(i)
        if mode:
           print(url)
        cpu_urls.append(url)
    return cpu_urls


def get_data_mainboard_page():
    motherboard = []
    page = [1, 2, 3, 4, 5, 6, 7]
    if mode:
        page = [1]
    for i in page:
        url = data['motherboard']
        url = url.format(i)
        if mode:
            print(url)
        motherboard.append(url)
    return motherboard

def get_data_GraphicsCard_page():
    graphics_card = []
    page = [1, 2, 3, 4, 5, 6, 7]
    if mode:
        page = [1]
    for i in page:
        url = data['Graphics_card']
        url = url.format(i)
        if mode:
            print(url)
        graphics_card.append(url)
    return graphics_card

def get_data_Memory_page():
    memory = []
    page = [1, 2, 3, 4, 5, 6, 7]
    if mode:
        page = [1]
    for i in page:
        url = data['Memory']
        url = url.format(i)
        if mode:
            print(url)
        memory.append(url)
    return memory

def get_data_HardDisk_page():
    Hard_disk = []
    page = [1, 2, 3, 4, 5, 6, 7]
    if mode:
        page = [1]
    for i in page:
        url = data['Hard_disk']
        url = url.format(i)
        if mode:
            print(url)
        Hard_disk.append(url)
    return Hard_disk

def get_data_HardDisk_url():
    Hard_disk = get_data_HardDisk_page()
    for url in Hard_disk:
        data = requests.get(url, headers=headers, cookies=cookies)
        data = bytes(data.text, 'utf-8').decode('unicode-escape')
        request = etree.HTML(data)
        GH.get_data_HardDisk_data(request, headers, cookies)

def get_data_Memory_url():
    memory = get_data_Memory_page()
    for url in memory:
        data = requests.get(url, headers=headers, cookies=cookies)
        data = bytes(data.text, 'utf-8').decode('unicode-escape')
        request = etree.HTML(data)
        GME.get_data_Memory_data(request,headers,cookies)

def get_data_GraphicsCard_url():
    graphics_card = get_data_GraphicsCard_page()
    for url in graphics_card:
        data = requests.get(url, headers=headers, cookies=cookies)
        data = bytes(data.text, 'utf-8').decode('unicode-escape')
        request = etree.HTML(data)
        GGC.get_data_GraphicsCard_data(request,headers,cookies)

def get_data_mainboard_url():
    motherboard = get_data_mainboard_page()
    for url in motherboard:
        data = requests.get(url, headers=headers, cookies=cookies)
        data = bytes(data.text, 'utf-8').decode('unicode-escape')
        request = etree.HTML(data)
        GM.get_data_motherboard_data(request,headers,cookies)

def get_data_cpu_url():
    cpu_urls = get_data_cpu_page()
    for url in cpu_urls:
        data = requests.get(url, headers=headers, cookies=cookies)
        if mode:
            print(data.status_code)
            #print(data.text)
        data = bytes(data.text, 'utf-8').decode('unicode-escape')
        request = etree.HTML(data)
        GC.get_data_cpu_data(request,headers,cookies)

